import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { Blade } from "../../../../shared/blade/blade.model";
import { Command } from "../../../../shared/toolbar.service";
import { DialogResult } from "../../../../shared/dialog.service";
import { ModelParam } from "../../../../entity/model-Param";
import { EditorParameterEditComponent } from './editor-parameter-edit/editor-parameter-edit.component';
import { ModelParamService } from "../../../../service/model-param.service";

@Component({
  selector: 'app-editor-parameter',
  templateUrl: './editor-parameter.component.html',
  styleUrls: ['./editor-parameter.component.css']
})
export class EditorParameterComponent extends BladeContainerBase<ModelParam> {
  /**
   * 型号参数集合
   * 
   * @type {ModelParam[]}
   * @memberof EditorParameterComponent
   */
  modelParams: ModelParam[] = [];
  /**
   * 型号参数name key值集合
   * 
   * @type {Setofvalue[]}
   * @memberof EditorParameterComponent
   */

  constructor(
    private modelParamSer: ModelParamService,
  ) {
    super()
  }

  ngOnInit() {
    this.commands = [
      new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
    ];
    super.ngOnInit();
  }
  initData(): any {
    this.modelParamSer.getModelParamsByModeID(this.parentEntity.modelId)
      .then(res => {
        if (res) {
          this.modelParams = [...res];
          this.currentEntity = this.modelParams[0];
          this.bladeData.isLoading = false;
        }

      })
      .catch(err => {
        console.log(err)
      })
  }

  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "editorParameterEdit";
      data.title = this.parentEntity.modelName;
      data.subtitle = "型号参数";
      let editKeyBlade = this.bladeService.createBlade(EditorParameterEditComponent, data, false, this.currentEntity, this.parentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  canExeEdit(blend?: Blade): boolean {
    return this.currentEntity != null;
  }

  /**
* 单选
* 
* @param {any} event 
* @param {any} key 
* 
* @memberof EditorParameterComponent
*/
  onItemCheack(event: HTMLInputElement, key: ModelParam) {
    key.isChecked = event.checked;
  }
  /**
   * 全选/全不选
   * 
   * @param {HTMLInputElement} event 
   * 
   * @memberof EditorParameterComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.modelParams.length > 0) {
      for (let i = 0; i < this.modelParams.length; i++) {
        this.modelParams[i].isChecked = event.checked;
      }
    }
  }
  onSeletedChange(selectedKey: ModelParam) {
    if (this.currentBlade.childrenCanDeactivate()) {
      this.currentEntity = selectedKey;
    }
  }
  /**
  * 获得选中项
  * 
  * @returns {ModelParam[]} 
  * 
  * @memberof EditorParameterComponent
  */
  getSelected(): ModelParam[] {
    if (this.modelParams.length > 0) {
      let filter = this.modelParams.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/model/editor-parameter/editor-parameter.component.ts